package com.gin.no_gay_alliance.adpter.follow;

import android.content.Context;
import android.view.View;

import androidx.annotation.NonNull;

import com.gin.jackson.utils.ObjectUtils;
import com.gin.nga.response.field.SimpleUserInfo;
import com.gin.no_gay_alliance.R;
import com.gin.no_gay_alliance.adpter.base.BaseBindingRecyclerViewAdapter;
import com.gin.no_gay_alliance.app.MyApplication;
import com.gin.no_gay_alliance.databinding.ItemFollowUserBinding;
import com.gin.no_gay_alliance.manager.FollowManager;

import java.util.List;

/**
 * 关注用户适配器
 *
 * @author : ginstone
 * @version : v1.0.0
 * @since : 2023/9/13 17:28
 **/
public class FollowUserAdapter extends BaseBindingRecyclerViewAdapter<ItemFollowUserBinding, SimpleUserInfo> implements View.OnClickListener {
    public FollowUserAdapter(Context context) {
        super(context);
    }

    public FollowUserAdapter(@NonNull Context context, @NonNull List<SimpleUserInfo> data) {
        super(context, data);
    }

    /**
     * 绑定数据
     *
     * @param holder
     * @param b        绑定对象
     * @param position 位置
     * @param item
     */
    @Override
    protected void onBindViewHolder(ViewHolder<ItemFollowUserBinding> holder, @NonNull ItemFollowUserBinding b, int position, SimpleUserInfo item) {
        String avatar = !ObjectUtils.isEmpty(item.getAvatars()) ? item.getAvatars().get(0) : "";
        b.tvUser.init(item.getUserId(), item.getUsername(), avatar, 36, 0);

        b.btFollow.setTag(item);
        b.btFollow.setOnClickListener(this);
    }

    /**
     * Called when a view has been clicked.
     *
     * @param v The view that was clicked.
     */
    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.bt_follow) {
            SimpleUserInfo user = (SimpleUserInfo) v.getTag();
            //取关
            FollowManager followManager = MyApplication.app.managerFactory.getFollowManager();
            followManager.unfollowUser(user.getUserId(), d -> {
                handler.toast(d.getMessage());
                int i = data.indexOf(user);
                data.remove(i);
                handler.post(() -> notifyItemRemoved(i));
            });
        }
    }
}
